<?php

/**
 * @author anhtung
 * @copyright 2013
 */

class Ideal extends CI_Controller{
    
    protected $mahoso;
    
    function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('userlayout');
        $this->userlayout->setLayout("user/layout");
        
        $userSession = $this->session->userdata('user');
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }
        $this->mahoso = $userSession['MaHoSo'];
        $this->load->model('user/ideal_model');
    }
    
    
    function createideal1(){
        $title = "Tạo ý tưởng";
        $currTemp = array('&nbsp;&nbsp; Ý tưởng >> Tạo ý tưởng');
        $current['current'] = $currTemp;
        $this->userlayout->view("user/createideal",null ,$title, $current);
    }
    
    function createideal(){
        $title = "Tạo ý tưởng";
        $currTemp = array('&nbsp;&nbsp;<a href= "'.base_url('nhanvien/ideal').'">Ý tưởng</a> &raquo; Tạo ý tưởng');
        $current['current'] = $currTemp;
        $this->load->helper('ckeditor');
        $data['ckeditor'] = array(
            'id'    =>      'idealContent',
            'path'  =>      'ckeditor',
            'config' => array(
                'toolbar'       =>      "Full",
                'width'         =>      "95%", 
                'height'        =>      '300px',),
            'styles' => array(
                'style 1' => array (
                    'name'          =>      'Blue Title',
                    'element'       =>      'h2',
                    'styles' => array(
                        'color'         =>      'Blue',
                        'font-weight'   =>      'bold')
                ),
                'style 2' => array (
                    'name'  =>      'Red Title',
                    'element'       =>      'h2',
                    'styles' => array(
                        'color'                 =>      'Red',
                        'font-weight'           =>      'bold',
                        'text-decoration'       =>      'underline'
                        )
                    )                              
            )
        );
        
        $this->userlayout->view("user/createideal",$data ,$title, $current);
        
    }
    
    function docreate(){
        $data = array();
        if(isset($this->mahoso) && !empty($this->mahoso)){
            $data['MaHoSo'] = $this->mahoso;
        }
        if(isset($_POST['titleideal']) && !empty($_POST['titleideal'])){
            $data['TieuDe'] = $_POST['titleideal'];
        }
        if(isset($_POST['content']) && !empty($_POST['content'])){
            $data['NoiDung'] = $_POST['content'];
        }
        $data['Ngay'] = date('Y-m-d H:i:s',time());
        if(isset($_POST['descripideal']) && !empty($_POST['descripideal'])){
            $data['MoTa'] = $_POST['descripideal'];
        }
        if(isset($_FILES['detailideal']) && ($_FILES['detailideal']['error'] == 0)){
        $uploaddir = APPROOT.'resources/ytuong/';
        $path = $_FILES['detailideal']['name'];
        $ext = pathinfo($path, PATHINFO_EXTENSION);
        $time = time();
        $file_name = "ytuong_hoso_".$this->mahoso."_".$time.".".$ext;
        $file_path = $uploaddir.$file_name;
        if(move_uploaded_file($_FILES['detailideal']['tmp_name'],$file_path)){
                
                $data['UrlFile'] = $file_name;
        }
        }
        if($this->ideal_model->create($data)){
            redirect('nhanvien/ideal');
            
        }
        
    }
    
    function index($sort = 'Ngay'){
        
        $this->load->library('pagination'); 
        $this->load->helper('url'); 
        $this->load->library('table'); 
             
            // cấu hình phân trang 
        $config['next_link'] = 'Next »'; 
        $config['prev_link'] = '« Prev'; 
        $config['num_links']	= 10;
        $config['base_url'] = base_url('nhanvien/ideal/index/'.$sort); // xác định trang phân trang 
        $config['total_rows'] = $this->ideal_model->count_all(); // xác định tổng số record 
        $config['per_page'] = 15; // xác định số record ở mỗi trang 
        $config['uri_segment'] = 5; // xác định segment chứa page number 
        $this->pagination->initialize($config); 
             
            // tạo table 
        $this->table->set_heading('MaYTuong','TieuDe'); 
        $dataIdeal = $this->ideal_model->list_all($config['per_page'],$this->uri->segment(5), $sort);
        $this->load->model('user/hoso_model');
        $dataHoso = array();
        foreach($dataIdeal as $ideal){
            $dataHoso[] = $this->hoso_model->getById($ideal['MaHoSo']);
        } 
        $data["links"] = $this->pagination->create_links();
        $data['data'] = $dataIdeal;
        $data['dataHoso'] = $dataHoso;
        $title = "Ý Tưởng";
        $currTemp = array('&nbsp;&nbsp;Ý Tưởng');
        $current['current'] = $currTemp;
        $this->userlayout->view("user/ideal_index",$data,$title, $current );
        //$this->load->view('user/ideal_index',$data); 
         
    }
    
    function detailideal($id = null){
        if($id != null){
            $title = "Trang chủ";
            $data['danhgia'] = false;
            $this->load->model("user/hoso_model");
            $result = $this->ideal_model->getById($id);
            $comment = $this->ideal_model->getCommentById($id);
            $resultComment = array();
            $tmp = $this->hoso_model->getById($this->mahoso);
            $data['user'] = $tmp[0]->HoTen;
            foreach($comment as $dataComment){
                $tmp = $this->hoso_model->getById($dataComment->MaHoSo);
                if($dataComment->MaHoSo == $this->mahoso && ($dataComment->DiemSangTao != null || $dataComment->DiemMoiLaDocDao != null || $dataComment->DiemTinhKhaDung != null || $dataComment->DiemPhanTich != null)){
                    $data['danhgia'] = true;
                }
                $dataComment->TenHoSo = $tmp[0]->HoTen;
                $dataComment->AnhDaiDien = $tmp[0]->AnhDaiDien;
                $resultComment[] = $dataComment;
            }
            
            $data['result'] = $result;
            $data['comments'] = $resultComment;
            $currTemp = array('&nbsp;&nbsp;<a href= "'.base_url('nhanvien/ideal').'">Ý tưởng</a> &raquo;Chi Tiết Ý Tưởng');
            $current['current'] = $currTemp;
            $this->userlayout->view("user/detail_ideal",$data,$title, $current );
        }
    }
    
    function docomment(){
        $data['MaHoSo'] = $this->mahoso;
        $diemDanhGia = 0;
        if(isset($_POST['diemdanhgia']) && !empty($_POST['diemdanhgia'])){
            $diemDanhGia = $_POST['diemdanhgia'];
        }
        if(isset($_POST['noidung']) && !empty($_POST['noidung'])){
            $data['NoiDung'] = $_POST['noidung'];
        }
        if(isset($_POST['maytuong']) && !empty($_POST['maytuong'])){
            $data['MaYTuong'] =  $_POST['maytuong'];
        }
        if(isset($_POST['diemsangtao']) && !empty($_POST['diemsangtao'])){
            $data['DiemSangTao'] =  $_POST['diemsangtao'];
            $diemDanhGia = $diemDanhGia + $_POST['diemsangtao'];
        }
        if(isset($_POST['diemmoiladocdao']) && !empty($_POST['diemmoiladocdao'])){
            $data['DiemMoiLaDocDao'] =  $_POST['diemmoiladocdao'];
            $diemDanhGia = $diemDanhGia + 2*$_POST['diemmoiladocdao'];
        }
        if(isset($_POST['diemtinhkhadung']) && !empty($_POST['diemtinhkhadung'])){
            $data['DiemTinhKhaDung'] =  $_POST['diemtinhkhadung'];
            $diemDanhGia = $diemDanhGia + 2*$_POST['diemtinhkhadung'];
        }
        if(isset($_POST['diemphantich']) && !empty($_POST['diemphantich'])){
            $data['DiemPhanTich'] =  $_POST['diemphantich'];
            $diemDanhGia = $diemDanhGia + $_POST['diemphantich'];
        }
        $data['Ngay'] = date('Y-m-d H:i:s',time());
        if(isset($data['NoiDung']) && !empty($data['NoiDung'])){
        $addComment = $this->ideal_model->addComment($data, $diemDanhGia);
        }
        redirect(base_url("nhanvien/ideal/detailideal/".$data['MaYTuong']));
        
    }
    
    function myIdeal($sort = 'Ngay'){
        $id = $this->mahoso;
        $this->load->library('pagination'); 
        $this->load->helper('url'); 
        $config['base_url'] = base_url('nhanvien/ideal/myIdeal/'.$sort);  
        $config['total_rows'] = $this->ideal_model->count_id($id); 
        $config['per_page'] = 2; 
        $config['uri_segment'] = 5;   
        $result = $this->ideal_model->list_sort($config['per_page'],$this->uri->segment(5), $id,$sort);
        $this->pagination->initialize($config); 
        $data['data'] = $result; 
        $data["links"] = $this->pagination->create_links();
        $title = "Ý Tưởng Của Tôi";
        $currTemp = array('&nbsp;&nbsp;<a href= "'.base_url('nhanvien/ideal').'">Ý tưởng</a> &raquo;Ý Tưởng Của Tôi');
        $current['current'] = $currTemp;
        $this->userlayout->view("user/myIdeal",$data,$title, $current );
    }
    
    function download($url){
        if($url != null){
            $download_path = $uploaddir = APPROOT.'resources/ytuong/';
            $url = urldecode($url);
            $file = $download_path.$url; 
            if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist."); 
 
            $type = filetype($file);  
            $today = date("F j, Y, g:i a"); 

            $time = time(); 
            header("Content-type: $type"); 
            header("Content-Disposition: attachment;filename=$filename"); 
            header("Content-Transfer-Encoding: binary"); 
            header('Pragma: no-cache'); 
            header('Expires: 0'); 
            set_time_limit(0); 
            readfile($file);  
        }
    }
    
}

?>